// pages/my/my.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {}
  },
  //查询登陆用户基本信息
  getUserInfoFunc: function () {
    //获取用户信息
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'user/getUserInfo'
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        // console.log('用户信息: ' + res.data.data)
        this.setData({
          user: res.data.data
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //跳转我的店铺
  getMyShop: function () {
    wx.navigateTo({
      url: '/pages/my-pages/myShop/myShop',
    })
  },
  //跳转我的会员
  getMember: function () {
    wx.navigateTo({
      url: '/pages/my-pages/myMember/member',
    })
  },
  //跳转消费项目
  getItemList: function () {
    wx.navigateTo({
      url: '/pages/my-pages/item-list/index',
    })
  },
  //跳转基础项目
  getBaseItemList: function () {
    wx.navigateTo({
      url: '/pages/my-pages/base-item-list/index',
    })
  },
  //跳转我的店员
  getStaff: function () {
    wx.navigateTo({
      url: '/pages/my-pages/staff/staff',
    })
  },
  //跳转店员数据
  getStaffData: function () {
    wx.navigateTo({
      url: '/pages/my-pages/staff-data/index',
    })
  },
  //跳转代金券列表
  getVoucherList: function () {
    wx.navigateTo({
      url: '/pages/my-pages/voucher-list/index',
    })
  },
  //跳转修改密码
  updatePassword: function () {
    wx.navigateTo({
      url: '/pages/my-pages/changePwd/changePwd',
    })
  },
  //待开发按钮提示
  muLovers: function () {
    ui.showToast('Coder正在加紧开发中')
  },
  //跳转个人资料
  myInfo: function () {
    wx.navigateTo({
      url: '/pages/my-pages/myInfo/myInfo',
    })
  },
  //更新头像弹窗
  changeAvatarBut() {
    console.log('点击了头像');
    let baseUrl = httpUtils.getBaseUrl();
    let other = this;
    wx.showModal({
      content: '选择图片上传更新头像',
      success: function (res) {
        if (res.confirm) {
          wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            sizeType: ['compressed'],
            camera: 'back',
            success(mediaRes) {
              wx.uploadFile({
                filePath: mediaRes.tempFiles[0].tempFilePath,
                name: 'file',
                url: baseUrl + '/file/miniUpload',
                formData: {},
                header: {
                  'Content-Type': 'application/json',
                  'X-Access-Token': wx.getStorageSync('token')
                },
                success: (res) => {
                  console.log(res)
                  if (res.statusCode == 200) {
                    let obj = {
                      method: 'POST',
                      showLoading: false,
                      url: 'user/changeAvatar',
                      data: {
                        'avatar': res.data
                      }
                    }
                    httpUtils.request(obj).then(res => {
                      console.log(res.data.message)
                      if (res.data.retCode != 0) {
                        ui.showToast(res.data.message)
                      } else {
                        other.getUserInfoFunc()
                      }
                    });
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  //退出登陆
  logout() {
    //获取用户信息
    let obj = {
      method: 'GET',
      showLoading: true,
      url: 'auth/logout'
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        // console.log('用户信息: ' + res.data.data)
        wx.setStorageSync('token', '');
        wx.reLaunch({
          url: '/pages/my-pages/login/login',
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();
    wx.hideShareMenu();
    this.getUserInfoFunc();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})